//
//  CustomTabView.swift
//  ZgwApp
//
//  Created by li on 2025-01-20.
//

import SwiftUI

struct FFPageView<D: RandomAccessCollection, P: View>: View where D.Element: Hashable  {
	
	
	init(selected: Binding<D.Element?>, data: D, @ViewBuilder content: @escaping (D.Element) -> P) {
		self.data = data
		self._selected = selected
		self.content = content
	}
	
	@Environment(\.fFPageViewTabPosition) var tabPosition: FFPageViewTabPosition
	@Environment(\.fFPageViewTab) var tabView: AnyView
	
	let data: D
	@Binding var selected: D.Element?
	
	var content: (D.Element) -> P
	
//	@State var contentScrollOffset: CGFloat = 0
	
	var body: some View {
		
		VStack(spacing: 0) {
			
			if tabPosition == .top {
				tabView
			}
			
			contentView()
			
			if tabPosition == .bottom {
				tabView

			}
		}
		
		
	}
	
	
	
	@ViewBuilder
	func contentView() -> some View {
		
		FFScrollPageView(selected: $selected, data: data) { item in
			content(item)
		}
		.ffScrollViewPagingEnabled(true)
	}
	
}

struct FFPageViewTestView: View {
//		@State var colors: [Color] = [.red, .green, .yellow, .gray, .black, .blue, .purple, .pink, .theme, .font666666DarkWhite, .tabBarBackgroundColorWhiteDark666666, .brown, .cyan, .indigo, .mint]
	@State var colors: [Color] = [.red, .green, .yellow]
	@State var selected: Color? = .green
	@State var position: FFPageViewTabPosition = .top
	var body: some View {
		
		FFPageView(selected: $selected, data: colors) { model in
			ZStack {
				model
				
				ScrollView {
					ForEach(0..<300) { index in
						Text("\(index)")
							.frame(height: 40)
					}
				}
			}
			.onAppear {
				print(model)
			}
		}
		.ffPageViewTab(position: position) {
			
			FFPageTabView(selected: selected, data: colors) { color in
				HStack {
					color
				}
				.frame(width: 80, height: 40)
				.onTapGesture {
					withAnimation {
						selected = color
					}
				}
			}
			.ffPageTabView(alignment: .center)
			.ffPageTabView(spacing: 20)
			
		}
		.onTapGesture {
			if position == .top {
				position = .bottom
			}else {
				position = .top
			}
		}
		
	}
}


#Preview {
	FFPageViewTestView()
}

